<template>
  <div class="validate-wrap">
    <div class="info-row">
      <el-card shadow="hover" class="total-row">
        <i class="el-icon-delete"></i>
        <div class="desc">
          <div class="text">总数据量</div>
          <div class="value">{{tempDataCount}}</div>
        </div>
      </el-card>
      <el-card shadow="hover" class="format-message">
        <i class="el-icon-delete"></i>
        <div class="desc">
          <div class="text">数据格式错误</div>
          <div class="value">{{formatErrorCount}}</div>
        </div>
      </el-card>
    </div>
    <div class="search-row">
      <el-checkbox v-model="onlyShowError" label="只显示错误数据" border @change="refreshList"></el-checkbox>
      <el-checkbox v-model="onlyShowMan" label="只显示需手动处理数据" border @change="refreshList"></el-checkbox>
    </div>
    <div class="operator-row">
      <el-button type="danger" class="clear-data" @click="clearTempData">清空数据重新上传</el-button>
      <el-button type="success" @click="startRunTest" :disabled="!formatErrorCount == 0">核对首贷户数据正确性</el-button>
      <el-button type="primary" @click="mergeTemp" :disabled="!mergeable">合并数据</el-button>
    </div>
    <el-table :data="list" stripe border style="width: 100%">
      <el-table-column prop="rowNum" label="excel中的行数" width="80"></el-table-column>
      <el-table-column prop="msg" label="差错信息"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div class="table-btn-row">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="reportDate" label="日期" width="100"></el-table-column>
      <el-table-column prop="reportOrg" label="银行" width="100"></el-table-column>
      <el-table-column prop="firmName" label="企业" width="180"></el-table-column>
      <el-table-column prop="companyScale" label="大中小类型" width="80"></el-table-column>
      <el-table-column prop="industryCategory" label="行业分类" width="100"></el-table-column>
      <el-table-column prop="shareholderType" label="控股性质" width="120"></el-table-column>
      <el-table-column prop="creditBalance" label="授信金额" width="120"></el-table-column>
      <el-table-column prop="loanBalance" label="贷款余额" width="120"></el-table-column>
      <el-table-column prop="online" label="线上" width="50"></el-table-column>
      <el-table-column prop="rate" label="利率" width="100"></el-table-column>
      <el-table-column prop="loanTermType" label="期限" width="70"></el-table-column>
      <el-table-column prop="guaranteeMethod" label="担保方式" width="120"></el-table-column>
      <el-table-column prop="firstLoanAreaCategory" label="贷款分县" width="120"></el-table-column>
      <el-table-column prop="uploadTime" label="上传时间" width="150"></el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      @current-change="changePage"
      @prev_click="changePage"
      @next_click="changePage"
    ></el-pagination>
  </div>
</template>
<script>
import {
  getFirstLoanTemp,
  getFormatErrorCount,
  getTempDataCount,
  clearTempData,
  compareToDatabase,
  checkMergeable,
  deleteTemp,
  merge
} from "@/api/first-loan.js";
import {
  Table,
  TableColumn,
  Button,
  Pagination,
  Card,
  Icon,
  Checkbox
} from "element-ui";
export default {
  name: "first-loan-validate",
  components: {
    [Table.name]: Table,
    [TableColumn.name]: TableColumn,
    [Button.name]: Button,
    [Pagination.name]: Pagination,
    [Card.name]: Card,
    [Icon.name]: Icon,
    [Checkbox.name]: Checkbox
  },
  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      onlyShowError: false,
      onlyShowMan: false,
      list: [],
      total: 0,
      formatErrorCount: 0,
      tempDataCount: 0,
      mergeable: false,
      runTestLoadingInstance: null,
      mergeLoadingInstance: null
    };
  },
  mounted() {
    this.refreshList();
  },
  methods: {
    changePage(pageNum) {
      this.pageNum = pageNum;
      this.refreshList();
    },
    refreshList() {
      getFirstLoanTemp({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        onlyShowError: this.onlyShowError,
        onlyShowMan: this.onlyShowMan
      })
        .then(res => {
          this.list = res.list;
          this.total = res.total;
        })
        .catch(errMsg => this.$alert(errMsg.msg));
      getFormatErrorCount()
        .then(res => (this.formatErrorCount = res.formatErrorCount))
        .catch(errMsg => this.$alert(errMsg.msg));
      getTempDataCount()
        .then(res => {
          this.tempDataCount = res.tempDataCount;
          if (this.tempDataCount == 0) {
            this.$emit("clearData");
          }
        })
        .catch(errMsg => this.$alert(errMsg.msg));

      checkMergeable()
        .then(res => {
          this.mergeable = res.mergeable;
        })
        .catch(errMsg => this.$alert(errMsg.msg));
    },
    clearTempData() {
      this.$confirm("清空数据后需要重新上传, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          clearTempData()
            .then(res => {
              this.$emit("clearData");
            })
            .catch(errMsg => {
              this.$alert(errMsg.msg);
              this.$emit("clearData");
            });
        })
        .catch(() => {});
    },
    startRunTest() {
      this.runTestLoadingInstance = this.$loading({
        fullscreen: true,
        text: "正在比对数据库"
      });
      compareToDatabase()
        .then(res => {
          this.refreshList();
          this.runTestLoadingInstance.close();
        })
        .catch(errMsg => {
          this.runTestLoadingInstance.close();
          this.$alert(errMsg.msg);
          this.refreshList();
        });
    },
    handleDelete(index, row) {
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteTemp(row.id)
            .then(res => {
              this.startRunTest();
            })
            .catch(errMsg => this.$alert(errMsg.msg));
        })
        .catch(() => {});
    },
    mergeTemp() {
      this.mergeLoadingInstance = this.$loading({
        fullscreen: true,
        text: "正在合并数据"
      });
      merge()
        .then(res => {
          this.refreshList();
          this.mergeLoadingInstance.close();
        })
        .catch(errMsg => {
          this.mergeLoadingInstance.close();
          this.$alert(errMsg.msg);
        });
    }
  }
};
</script>
<style lang="stylus" scoped>
.validate-wrap {
  padding-left: 5px;

  .info-row {
    margin-bottom: 5px;
    margin-top: 5px;
    height: 100px;
    background-color: #eee;

    .el-card {
      position: relative;
      display: inline-block;
      margin-right: 30px;
      top: 50%;
      transform: translateY(-50%);
      width: 230px;

      i {
        font-size: 28px;
      }

      .desc {
        float: right;
        text-align: center;

        .text {
          color: #666;
          font-size: 14px;
          margin-top: -10px;
          margin-bottom: 5px;
        }

        .value {
          font-size: 20px;
          font-weight: bolder;
        }
      }
    }
  }

  .search-row {
    margin-bottom: 5px;
  }

  .operator-row {
    margin-bottom: 5px;
  }

  .table-btn-row {
    width: 60px;

    .el-button {
      margin-left: 0px;
      margin-bottom: 3px;
    }
  }
}
</style>